# -*- coding: utf-8 -*-
from django.db import models
from django.db.models import signals
from django.core.urlresolvers import reverse
from django.template.defaultfilters import slugify
from Scope.thumbs import ImageWithThumbsField

def content_image_name(instance, filename):
    return '/'.join(['arquivos/igreja', str(instance.nomeSetor.slugdivisao), filename])

class Igreja(models.Model):
    nome = models.CharField("Denominação", max_length=50, unique=True)
    slugigreja = models.SlugField(max_length=50, blank=True, unique=True)
    def __unicode__(self):
        return self.nome
    def get_absolute_url(self):
        return reverse('Scope.views.igreja', kwargs={'slugigreja': self.slugigreja})

class Divisao(models.Model):
    setor = models.CharField("Zona", max_length=15, unique=True)
    cidade = models.CharField(max_length=30, default="Teresina")
    nomeIgreja = models.ForeignKey(Igreja)
    slugdivisao = models.SlugField(max_length=15, blank=True, unique=True)
    def __unicode__(self):
        return self.setor
#    def get_absolute_url(self):
#        return reverse('Scope.views.setor', kwargs={'nomeigreja': self.nomeIgreja.slugigreja, 'slugdivisao': self.slugdivisao})

class Congregacao(models.Model):
    nome = models.CharField("Nome da congregação", max_length=50, unique=True)
    nomeIgreja = models.ForeignKey(Igreja)
    nomeSetor = models.ForeignKey(Divisao)
    supervisor = models.CharField(max_length=30)
    endereco = models.CharField("Endereço", max_length=70)
    telefone = models.CharField("Telefone", max_length=10, blank=True, null=True)
    googlemaps = models.TextField("Embed Maps", blank=True, null=True)
    fotoigreja = ImageWithThumbsField('Imagem Destaque', upload_to=content_image_name, sizes=((800,600),(300,225)))
    slugcongregacao = models.SlugField(max_length=200, blank=True, unique=True)
    def __unicode__(self):
        return self.nome
    def get_absolute_url(self):
        return reverse('Scope.views.congregacao', kwargs={'slugigreja': self.nomeIgreja.slugigreja, 'slugcongregacao': self .slugcongregacao})

def igreja_pre_save(signal, instance, sender, **kwargs):
    instance.slugigreja = slugify(instance.nome)

signals.pre_save.connect(igreja_pre_save, sender=Igreja)

def divisao_pre_save(signal, instance, sender, **kwargs):
    instance.slugdivisao = slugify(instance.setor)

signals.pre_save.connect(divisao_pre_save, sender=Divisao)

def congregacao_pre_save(signal, instance, sender, **kwargs):
    instance.slugcongregacao = slugify(instance.nome)

signals.pre_save.connect(congregacao_pre_save, sender=Congregacao)